Yacon.widget.TForm = function(conText){
	
	Yacon.apply(this,conText);
	
	if(this.view){
		this.element = this.view;
		this.elementID = this.view.id;
		this.elementType = this.view.type;
	}
	
	this.items = new Yacon.container.Record();
	
	this.dropItems = new Yacon.container.Record();
	
};

Yacon.widget.TForm.prototype = {
		
	getHtml: function(){
		
		var html = "";
		
		html += "<div class='row-ht-fix'>"
		
		html += "<form class='form-horizontal' role='form' ";
		
		html += " lily-id='"+this.view.id+"' lily-type='form' ";
		
		html += " > ";
				
		this.layout = this.view.layout;
		
		var rowSize = this.layout.length;
		for(var i=0;i<rowSize;i++){
			
			var activeRow = this.layout[i];
			var colSize = activeRow.length;
			if(colSize <= 0){
				continue;
			}
			
			html += "<div class='form-group'>";
			
			for(var j=0;j<colSize;j++){
				
				var cell = activeRow[j];
				var comSize = cell.component.length;
				
				var className = "col-md-"+cell.width+" ";
				if(!Yacon.isEmpty(cell.offset)){
					className += " col-md-offset-"+cell.offset+" "
				}
				
				
				html += "<div class='"+className+"'>";
				
				for(var x=0;x<comSize;x++){
					
					var component = cell.component[x];
					
					html += this.getComponentHtml(component);
					
				}
				
				html += "</div>";
			}
			
			html += "</div>";
			
		}
		
		html += "</form>";
		
		html += "</div>";
		
		return html;
		
	},
	
	/* 获取组件HTML 部分信息 */
	getComponentHtml: function(component,plusHtml){
		
		var html = "";
		
		if(component.kind == "input"){		
			var activeElement = new Yacon.widget.TInput(component);
			activeElement.parent = this;
			
			this.items.setFieldValue(component.name,activeElement);
			
			html += activeElement.getHtml(plusHtml);			
		}

		else if(component.kind == "drop"){
			
			var activeElement = new Yacon.widget.TDrop(component);
			activeElement.parent = this;
			
			this.items.setFieldValue(component.name,activeElement);
			
			//联动存储信息
			if(!Yacon.isEmpty(component.relaID)){
				this.dropItems.setFieldValue(component.relaID,activeElement);
			}
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "dropdate"){
			
			var activeElement = new Yacon.widget.TDropDate(component);
			activeElement.parent = this;
			
			html += activeElement.getDateHtml(plusHtml);
			
		}
		
		else if(component.kind == "droptime"){
			
			var activeElement = new Yacon.widget.TDropDate(component);
			activeElement.parent = this;
			html += activeElement.getTimeHtml(plusHtml);
			
		}
		
		else if(component.kind == "droptimestamp"){
			
			var activeElement = new Yacon.widget.TDropDate(component);
			activeElement.parent = this;	
			
			html += activeElement.getDateTimeHtml(plusHtml);
			
		}
		
		else if(component.kind == "textarea"){
			
			var activeElement = new Yacon.widget.TTextArea(component);
			activeElement.parent = this;
			
			this.items.setFieldValue(component.name,activeElement);
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "button"){
			html += "&nbsp;&nbsp;&nbsp;";
			
			var activeElement = new Yacon.widget.TButton(component);
			activeElement.parent = this;
			html += activeElement.getFormHtml(plusHtml);
			
		}
		
		else if(component.kind == "check"){
			
			var activeElement = new Yacon.widget.TCheck(component);
			activeElement.parent = this;
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "radio"){
			
			var activeElement = new Yacon.widget.TRadio(component);
			activeElement.parent = this;
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "label"){
			
			var activeElement = new Yacon.widget.TLabel(component);
			activeElement.parent = this;
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "progress"){
			
			var activeElement = new Yacon.widget.TProgress(component);
			activeElement.parent = this;
			
			
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		else if(component.kind == "file"){
			
			var activeElement = new Yacon.widget.TFile(component);
			
			activeElement.parent = this;
			
			this.items.setFieldValue(component.name,activeElement);
			
			html += activeElement.getHtml(plusHtml);
			
		}
		
		return html;
	},
	
	addEvents: function(){
		
		Yacon.callBack(this.elementID+"_beforeEvent", [this,this.page]);
		
		var self = this;
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		
		$(search+" button[lily-player='button']").unbind("click");
		$(search+" button[lily-player='button']").click(function(e){		
			
			var button = new Yacon.widget.TButton($(this));
			
			button.page = self.page;
			button.component = self;
			
			button.addEvent();	
			
		});
		
		var dateEles = $(search).find(".date");
		if(dateEles.length > 0){		
			this.dropdate = new Yacon.widget.TDropDate();		
			this.dropdate.addEvent();
		}
		
		for(var i=0;i<this.items.getFieldCount();i++){
			this.items.getFieldValue(i).addEvent();
		}
		
		Yacon.callBack(this.elementID+"_afterEvent", [this,this.page]);
		
	},
	
	layoutBefore : function(){
		
	},
	
	instData : function(dataSet){
		
		if(Yacon.isEmpty(dataSet)){
			dataSet = this.parent.dataSet;
		}
		
		if(Yacon.isEmpty(dataSet)){
			return;
		}
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";	
		var alls = $(search+" .lily-field");
		
		if(alls){
			
			var size = alls.length;		
			
			for(var i=0;i<size;i++){
			
				var active = alls.eq(i);
				var fieldName = active.attr("field-name");
				var fieldValue = active.val();
				
				if(Yacon.isNull(fieldValue)){
					fieldValue = "";
				}
				
				//复选框
				var fieldTyp = active.attr("field-type");
				if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "check"){
					var allChecks = active.find("input[type='checkbox']:checked");
					
					for(var j=0;j<allChecks.length;j++){
						fieldValue += ","+allChecks.eq(j).attr("field-key") ;
					}
					
					if(fieldValue.startsWith(",")){
						fieldValue = fieldValue.substring(1);
					}
				}
							
				//单选框
				if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "radio"){
					var allChecks = active.find("input[type='radio']:checked");
					for(var j=0;j<allChecks.length;j++){
						fieldValue += ","+allChecks.eq(j).attr("field-key") ;
					}
					
					if(fieldValue.startsWith(",")){
						fieldValue = fieldValue.substring(1);
					}
					
				}
				
				//Label信息
				if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "label"){
					fieldValue = active.html();
				}
				
				var fieldOldValue = fieldValue;
				
				dataSet.setBody(fieldName,fieldValue);
				
			}
			
		}
		
	},
	
	/* 重置 */
	reset: function(dataSet){
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		
		var forms = $(search);
		
		if(forms.length >= 1){
			forms[0].reset();
		}
		
		/* 单独信息重置 */
		var lilyFields = $(search+" .lily-field");
		for(var i=0;i<lilyFields.length;i++){
			
			var active = lilyFields.eq(i);
			var fieldTyp = active.attr("field-type");
			
			if(fieldTyp == "check"){
				
				var allChecks = active.find("input[type='checkbox']:checked");
				
				for(var j=0;j<allChecks.length;j++){
					allChecks.eq(j).prop("checked","false");
				}
				
			}
			
			else if(fieldTyp == "radio"){
				
				var allChecks = active.find("input[type='radio']:checked");
				
				for(var j=0;j<allChecks.length;j++){
					allChecks.eq(j).prop("checked","false");
				}
			}

			else if(fieldTyp == "label"){
				active.html("");
			}
			
			else if(fieldTyp == "date"){
				active.val("");
			}
			
		}
		
		this.instData(dataSet);
		
	},
	
	/* 值 */
	value: function(fieldName,value){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(value != undefined){
			activeField.val(value);
			return value;
		}else{
			return activeField.val();
		}
		
	},
	
	text: function(fieldName,value){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(value != undefined){
			activeField.text(value);
			return value;
		}else{
			return activeField.text();
		}
	},
	
	html: function(fieldName,value){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(value != undefined){
			activeField.html(value);
			return value;
		}else{
			return activeField.html();
		}
	},
	
	select: function(fieldName,fieldValue,fieldText){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(fieldValue == undefined){
			return activeField.find("option[value='"+fieldValue+"'][selected='true']").val();
		}
		
		if(fieldText == undefined){
			fieldText = fieldValue;
		}
		
		if(activeField.find("option[value='"+fieldValue+"']").length <= 0){
			activeField.append("<option value='"+fieldValue+"'>"+fieldText+"<option>");
		}
		
		activeField.find("option[value='"+fieldValue+"']").eq(0).prop("selected","true");
		
	},
	
	disabled: function(fieldName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		var parent = activeField.parent().parent();
		
		activeField.prop("disabled","true");
		
		parent.find(".bk-bg-primary").removeClass("bk-bg-primary").addClass("bk-bg-brown");
		
		
	},
	
	check: function(fieldName,fieldValue){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(fieldValue == undefined){
			
			fieldValue = "";
			
			var fieldTyp = activeField.attr("field-type");
			
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "check"){
				
				var allChecks = activeField.find("input[type='checkbox']:checked");
				for(var j=0;j<allChecks.length;j++){
					fieldValue += ","+allChecks.eq(j).attr("field-key") ;
				}
				
				if(fieldValue.startsWith(",")){
					fieldValue = fieldValue.substring(1);
				}	
			}
			
			return fieldValue;
		}
		
		else{
			
			var fieldTyp = activeField.attr("field-type");
			
			/* 复选框 */
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "check"){
				var item = fieldValue.split(",");
				for(var j=0;j<item.length;j++){					
					var itemVal = item[j];				
					activeField.find("input[field-key='"+itemVal+"']")
					         .prop("checked","true");				
				}			
			}
			
		}
		
	},
	
	radio: function(fieldName,fieldValue){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(fieldValue == undefined){
			
			fieldValue = "";
			
			var fieldTyp = activeField.attr("field-type");
			
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "radio"){
				
				var allChecks = activeField.find("input[type='radio']:checked");
				for(var j=0;j<allChecks.length;j++){
					fieldValue += ","+allChecks.eq(j).attr("field-key") ;
				}
				
				if(fieldValue.startsWith(",")){
					fieldValue = fieldValue.substring(1);
				}	
			}
			
			return fieldValue;
		}
		
		else{
			
			var fieldTyp = activeField.attr("field-type");
			
			/* 复选框 */
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "radio"){
				
				activeField.find("input[field-key='"+fieldValue+"']")
				           .prop("checked","true");				
						
			}
			
		}
		
	},
	
	progress: function(fieldName,fieldValue){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		if(fieldValue == undefined){
			
			fieldValue = "";
			
			var fieldTyp = activeField.attr("field-type");
			
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "progress"){
				
				fieldValue = activeField.attr("aria-valuenow");
				
			}
			
			return fieldValue;
		}
		
		else{
			
			if(Yacon.isString(fieldValue)){
				if(fieldValue.endWith("%")){
					fieldValue = fieldValue.substring(0,fieldValue.length-1);
				}
			}
			
			var fieldTyp = activeField.attr("field-type");
			
			/* 复选框 */
			if(!Yacon.isEmpty(fieldTyp) && fieldTyp == "progress"){
				
				activeField.attr("aria-valuenow",fieldValue);
				activeField.css("width",fieldValue+"%");
				
				activeField.html(fieldValue+"%");
						
			}
			
		}
		
		
	},
	
	hide:function(fieldName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']").parent();
		
		activeField.css("display","none");
	},
	
	show:function(fieldName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']").parent();
		
		activeField.css("display","");
	},
	
	empty:function(fieldName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		activeField.empty();
		
	},
	
	getJQueryObject:function(fieldName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		return activeField;
	},
	
	find: function(itemID){
		return this.items.getFieldValue(itemID);
	},
	
	trigger : function(fieldName,functionName){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		activeField.trigger(functionName);
	},
	
	fontColor : function(fieldName,color){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		activeField.css("color",color);
	},
	
	backColor　: function(fieldName,color){
		
		fieldName = fieldName.toUpperCase();
		
		var search = this.page.element.search + " form[lily-id='"+this.elementID+"'][lily-type='"+this.elementType+"']";
		var activeField = $(search+" .lily-field[field-name='"+fieldName+"']");
		
		activeField.css("background-color",color);
	
	},
	
	ico : function(icoName){
		
		var html = "";	
		html += "<span class='"+icoName+"'>";	
		html += "</span>";		
		return html;
	}
};